using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using MM.Business;
using MM.Entities;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using BarcodeLib;

namespace MM
{
    public partial class frmXemThongTinCaNhan : Form
    {
        public static string strConn;
        public static int maTaiKhoan;
        public BarcodeLib.Barcode bar = new BarcodeLib.Barcode();

        public frmXemThongTinCaNhan(string sConn)
        {
            InitializeComponent();
            strConn = sConn;
            maTaiKhoan = frmDangNhap.objTaiKhoan._maTaiKhoan;
            this.LoadForm();

            Common.SetColor(this);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void LoadForm()
        {
            clsTAI_KHOAN clsTk = new clsTAI_KHOAN(strConn);
            clsCHI_TIET_CD_TK clsCdTk = new clsCHI_TIET_CD_TK(strConn);
            clsCHUC_DANH clsCd = new clsCHUC_DANH(strConn);
            clsTAU clsT = new clsTAU(strConn);
            TAI_KHOAN tkDTO = new TAI_KHOAN();
            List<CHI_TIET_CD_TK> lstCdTk = new List<CHI_TIET_CD_TK>();
            CHUC_DANH cdDTO = new CHUC_DANH();
            TAU tDTO = new TAU();

            tkDTO = clsTk.LoadTaiKhoan(maTaiKhoan);
            txtCmnd.Text = tkDTO._soCMND;
            txtHoTen.Text = tkDTO._hoTen;
            txtMaTaiKhoan.Text = tkDTO._maTaiKhoan.ToString();
            tDTO = clsT.LoadTauByMaTau(tkDTO._maTau);
            txtTenTau.Text = tDTO._tenTau;

            int i = 0;
            lstCdTk = clsCdTk.SelectCHI_TIET_CD_TKByMaCDVaMaTK(-1, maTaiKhoan);
            if (lstCdTk.Count > 0)
            {
                foreach (CHI_TIET_CD_TK CdTkDTO in lstCdTk)
                {
                    i++;
                    cdDTO = clsCd.LoadChucDanhByMaCD(CdTkDTO._maChucDanh);
                    grdDinhDanh.Rows.Add(i, cdDTO._tenChucDanh, CdTkDTO._ngayBatDau.ToString(), CdTkDTO._ngayKetThuc.ToString(), cdDTO._maChucDanh.ToString());
                }
            }

            this.InitBarCode();
        }

        private void grdDinhDanh_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            clsQUYEN clsQ = new clsQUYEN(strConn);
            List<QUYEN> lstQ = new List<QUYEN>();
            int maCd;

            try
            {
                grdPhanQuyen.Rows.Clear();

                if (grdDinhDanh.Rows[e.RowIndex].Cells["MaChucDanh"].Value.ToString() != null)
                {
                    maCd = int.Parse(grdDinhDanh.Rows[e.RowIndex].Cells["MaChucDanh"].Value.ToString());
                    lstQ = clsQ.LoadPhanQuyenCu(maCd);
                    if (lstQ.Count > 0)
                    {
                        int i = 0;
                        foreach (QUYEN qDTO in lstQ)
                        {
                            i++;
                            grdPhanQuyen.Rows.Add(i, qDTO._tenForm, qDTO._moTa);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void cmdInThe_Click(object sender, EventArgs e)
        {
            List<TAI_KHOAN> lstTK = new List<TAI_KHOAN>();
            clsTAI_KHOAN clsTk = new clsTAI_KHOAN(strConn);
            TAI_KHOAN tkDTO = new TAI_KHOAN();   
            tkDTO = clsTk.LoadTaiKhoan(maTaiKhoan);
            if (tkDTO != null && tkDTO._soCMND != "")
            {
                bar.Encode(TYPE.CODE128, tkDTO._soCMND);
                tkDTO.BarCode = bar.GetImageData(SaveTypes.BMP);
                this.crystalReportViewer1.Visible = true;
                lstTK.Add(tkDTO);
                TheNhanVien rpt = new TheNhanVien();
                rpt.SetDataSource(lstTK);
                crystalReportViewer1.ReportSource = rpt;
            }
        }

        public void InitBarCode()
        {
            bar.BackColor = Color.White;
            bar.ForeColor = Color.Black;
            bar.IncludeLabel = false;
            bar.Width = 1000;
            bar.Height = 200;
            bar.Alignment = AlignmentPositions.LEFT;

        }
    }
}